package com.cloud.user.exception;

import com.cloud.common.R;
import com.cloud.excption.BaseException;
import com.cloud.excption.BizException;
import com.cloud.excption.ValidException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * 咕泡学院，只为更好的你
 * 咕泡学院-Mic: 2227324689
 * http://www.gupaoedu.com
 **/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public R handleException(Exception e, HttpServletRequest request) {
        log.info("GlobalExceptionHandler.handleException:{},{}", request.getRequestURI(), e);
        String msg = "系统繁忙：" + e.getMessage();
        if (e instanceof ValidException || e instanceof BizException) {
            msg = ((BaseException) e).getMsg();
        }
        return new R.Builder().buildCustomize(msg);
    }
}
